package showtime.framework.manager
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;
	
	import showtime.framework.utils.ClassUtility;
	import showtime.framework.ui.IWindow;

	/**
	 * @project Client
	 * @date 2011-7-5
	 * @author 绿豆蛙
	 * 
	 */
	public final class WindowManager
	{
		private static var cache:Dictionary = null;
	
		private static var modalWin:DisplayObject = null;
		
		{
			cache = new Dictionary();
		}
		
		/**
		 * 是否已经存在该窗口的实例对象。
		 *  
		 * @param window
		 * @return 
		 * 
		 */		
		public static function hasWindow(window:*):Boolean
		{
			if (cache.hasOwnProperty(ClassUtility.getClassName(window)))
			{
				return true;
			}
			
			return false;
		}
		
		public static function getWindow(clazz:Class):DisplayObject
		{
			if (hasWindow(clazz))
			{
				return cache[ClassUtility.getClassName(clazz)];
			}
		
			return null;
		}
		
		/**
		 * 将面板添加到父容器中，如果缓存中没有则创建。 
		 * 
		 */		
		public static function createPopUp(clazz:Class, parent:DisplayObjectContainer, modal:Boolean = false):DisplayObject
		{	
			var window:* = null;
			
			if (modal)
			{
				if (modalWin == null)
				{
					modalWin = new DefaultModalWindow();
				}
				
				parent.addChild(modalWin);	
			}
			
			if (hasWindow(clazz))
			{
				window = getWindow(clazz);
				parent.addChild(window);
			}
			else
			{	
				window = new clazz();
				cache[ClassUtility.getClassName(clazz)] = window;				
				parent.addChild(window);
			}			
			
			
			if (window is IWindow)
			{
				window.open();
			}
			
			return window;
		}
		
		
		/**
		 * 销毁窗体。
		 * 
		 * @param className
		 * 
		 */		
		public static function dispose(clazz:Class):void
		{
			removePopUp(clazz);
			
			delete cache[ClassUtility.getClassName(clazz)];
		}
		
		public static function centerPopUp(window:DisplayObject):IWindow
		{
			if (window.parent != null)	
			{
				window.x = (window.parent.width - window.width) * 0.5;
				window.y = (window.parent.height - window.height) * 0.5;
				
			}
			
			return IWindow(window);
		}
		
		/**
		 * 关闭窗体。
		 *  
		 * @param window
		 * 
		 */		
		public static function removePopUp(clazz:Class):void
		{
			if (hasWindow(clazz))
			{
				var window:* = getWindow(clazz);
				
				if (window.parent != null)
				{
					if (window is IWindow)
					{
						window.close();
					}
					window.parent.removeChild(window);
				}
				
				modalWin != null && modalWin.parent != null && modalWin.parent.removeChild(modalWin);
		
			}
		}
		
	
	}
}
import flash.display.Sprite;
import flash.events.Event;

internal class DefaultModalWindow extends Sprite
{
	public function DefaultModalWindow()
	{
		addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		
	}
	
	private function addedToStageHandler(evt:Event):void
	{
		graphics.beginFill(0x000000, 0);		
		graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
	}
}